package com.ssm.sfzy.controller;
import com.ssm.sfzy.constant.CommonConstant;
import com.ssm.sfzy.entity.Article;
import com.ssm.sfzy.service.ArticleService;
import com.ssm.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("/find")
    public Object find(
            @RequestParam(defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
            @RequestParam(defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
            Article article
    ){
        Map<String, Object> map = articleService.findPage(pageNum, pageSize, article);
        return  Result.success(map);
    }


    @GetMapping("/findById/{id}")
    public Object findById(@PathVariable Integer id){
        Article article = articleService.findById(id);
        return Result.success(article);
    }


    @PostMapping("/add")
    public Object add(@RequestBody Article article) throws Exception {
        articleService.add(article);
        return Result.success(article);
    }

    @PutMapping("/edit")
    public Object edit(@RequestBody Article article) throws Exception {
        articleService.edit(article);
        return Result.success(article);
    }
    @DeleteMapping("/remove/{id}")
    public Object remove(@PathVariable Integer id){
        articleService.remove(id);
        return Result.success(id);
    }

    @DeleteMapping("/removeBatch")
    public Object removeBatch(Integer[] ids){
        articleService.removeBatch(ids);
        return Result.success(ids);
    }








}
